﻿using System;
using System.Linq;
using System.Collections.Generic;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil.CustomObject;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;
using System.ComponentModel;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Numeric
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityD0400", Name = "计量类项目检证", CategoryType = EnumCategoryType.数值, Model = true)]
    public class VerfityD0400 : VerfityOneResult
    {   
        private CustomCollection<DataRange> _rangelist = new CustomCollection<DataRange>() { Description = "检查对照表数据" };

        [Category("检查项目对照表"), Description("输入检查项目对照项目")]
        [TypeConverter(typeof(CustomCollectionConverter<DataRange>))]
        [RefreshProperties(RefreshProperties.All)]
        public CustomCollection<DataRange> Rangelist
        {
            get { return _rangelist; }
            //set { _rangelist = value; }
        }

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            if (entryCommon.Equals("30"))
            {
                return false;
            }

            bool bresult = false;

            bresult = Rangelist.Any(rang => CheckRange(result, rang));

            if (bresult)
            {
                System.Console.WriteLine("VerfityD0400");
            }

            return bresult;
        }

        private bool CheckRange(List<List<string>> result, DataRange range)
        {
            if (range.Row == -1 && range.Col == -1)
            {
                return result.SelectMany(o => o).Any(t => CompareRange(t, range));
            }
            else if (range.Row == -1)
            {
                return result.Any(row => row.Count > range.Col && CompareRange(row[range.Col], range));
            }
            else if (range.Col == -1)
            {
                return result.Count > range.Row && result[range.Row].Any(col => CompareRange(col, range));
            }
            else
            {
                return result.Count > range.Row && result[range.Row].Count > range.Col && CompareRange(result[range.Row][range.Col], range);
            }
        }

        private bool CompareRange(string entryResult, DataRange range)
        {
            double entryValue;
            if (!double.TryParse(entryResult, out entryValue))
            {
                return false;
            }

            bool bCompare = false;

            bCompare = entryValue < range.minValue || entryValue > range.maxValue;

            return bCompare;
        }
    }
}